/**********************************************************
 * Copyright © 2013-1014 深圳市美传网络科技有限公司版权所有
 * 创 建 人：gumengfu
 * 创 建 日 期：2014-11-5 下午12:01:53
 * 版 本 号：
 * 修 改 人：
 * 描 述：
 * <p>
 *	
 * </p>
 ***********************************************************/
package com.daixu.xutil.demo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.daixu.xutil.demo.widget.swipebacklayout.SwipeBackLayout;
import com.daixu.xutil.demo.widget.swipebacklayout.app.SwipeBackFragmentActivity;

import org.xutils.x;

import java.util.List;

/**
 * <p>
 * 
 * </p>
 * 
 * @author yangbo
 * @date 2014-11-5
 * @version 1.0
 * @since 1.0
 */
public abstract class BaseFragmentActivity extends SwipeBackFragmentActivity {

	protected static final int REQUEST_CODE_1 = 0x00000001;

	protected static final int REQUEST_CODE_2 = 0x00000002;

	private static final int VIBRATE_DURATION = 20;

	private SwipeBackLayout mSwipeBackLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		x.view().inject(this);
		setTheme(R.style.AppBaseTheme);
		AppLoader.getInstance().addActivity(this);
		
		mSwipeBackLayout = getSwipeBackLayout();
		mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
		mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
			@Override
			public void onScrollStateChange(int state, float scrollPercent) {

			}

			@Override
			public void onEdgeTouch(int edgeFlag) {
				vibrate(VIBRATE_DURATION);
			}

			@Override
			public void onScrollOverThreshold() {
				vibrate(VIBRATE_DURATION);
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		restoreTrackingMode();
	}

	private void restoreTrackingMode() {
		mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
	}

	private void vibrate(long duration) {
		Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		long[] pattern = { 0, duration };
		vibrator.vibrate(pattern, -1);
	}

	@Override
	protected void onDestroy() {
		AppLoader.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		List<Fragment> fragments = getSupportFragmentManager().getFragments();
		for (Fragment fragment : fragments) {
			if (fragment != null && fragment.isVisible()) {
				fragment.onActivityResult(requestCode, resultCode, data);
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/*******************************************************
	 * 说明：显示网络错误的对话框
	 * 
	 * @author yangbo
	 *******************************************************/
	protected void showNetworkErrorDialog() {
//		CustomDialog dialog = new CustomDialog.Builder(this)
//				.setTitle(R.string.network_error_title)
//				.setMessage(R.string.network_error_msg)
//				.setPositiveButton(R.string.setting_network, new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int position) {
//						dialog.dismiss();
//						Intent wifiSetting = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
//						startActivity(wifiSetting);
//					}
//				})
//				.setNegativeButton(R.string.cancel, null).create();
//		dialog.show();
	}

	/*******************************************************
	 * 说明：快速设置标题名，标题组件的的id必须为text_title;
	 * 
	 * @author yangbo
	 * @param text
	 ********************************************************/
	protected void setTitleText(CharSequence text) {
		TextView textView = (TextView) findViewById(R.id.tv_title);
		if (textView != null) {
			textView.setText(text);
		}
	}

	/*******************************************************
	 * 说明：快速设置标题名，标题组件的的id必须为text_title;
	 * 
	 * @author yangbo
	 * @param resId
	 ********************************************************/
	protected void setTitleText(int resId) {
		TextView textView = (TextView) findViewById(R.id.tv_title);
		if (textView != null) {
			textView.setText(resId);
		}
	}

	protected void showBackButton() {
		Button button = (Button) findViewById(R.id.btn_back);
		if (button != null)
			button.setVisibility(View.VISIBLE);
	}

	public void doBack(View v) {
		super.onBackPressed();
		//finish();
	}

}
